package com.itheima.ui;

import java.util.Random;

public class CodeUtil {
    public static String getCode() {
        Random r = new Random();


        //查表法
        char[] chs = new char[52];
        //循环遍历将字母存入数组
        for (int i = 0; i < chs.length; i++) {
            if(i<=25){
                chs[i]=(char)(i+'a');
            }else{
                chs[i]=(char)(i-26+'A');
            }
        }
        //循环出5个字符，后面直接拿随机数字覆盖随机位置的字母
        String code = "";
        for (int i = 0; i < 5; i++) {
            code += chs[r.nextInt(chs.length)];
        }

        //随机数字和索引
        int number = r.nextInt(10);
        int index = r.nextInt(5);

        //把code字符串转成字符数组
        char[] charArray = code.toCharArray();

        //把随机数字覆盖随机位置的字母
        charArray[index] = (char) (number + '0');

        //返回
        return new String(charArray);
    }
}
